﻿using UnityEngine;


public enum SwordType
{
    Normal,
    Bounce, //弹力剑
    Pierce,  //穿透剑
    Spin,   //旋转剑    
}
public class SwordSkill : Skill
{
    [Header("Bounce info")]
    public int bounceAmount = 4;
    public float bounceSpeed = 10f;
    public float bounceGravity;

    [Header("Pierce info")]
    public int pierceAmount = 2;
    public float priceGravity;

    [Header("Spin info")]
    public float maxTravelDistance = 10f;
    public float spinDuration = 10f;
    public float hitCooldown = 5f;
    public float spinGravity;

    [Header("Skill info")]
    public GameObject swordPrefab;
    public Vector2 launchForce;
    public float gravityScale;
    private Vector2 finalDir;
    public float freezeTime;

    [Header("Aim dots")]
    [SerializeField] private int dotsNum;
    [SerializeField] private float disBetweenDots;
    [SerializeField] private GameObject dotPrefab;
    [SerializeField] private Transform dotsParent;

    GameObject[] dots;
    float dir;
    public SwordType swordType;

    protected override void Start()
    {
        base.Start();
        CreateDots();
        SetSwordGavity();
    }

    void SetSwordGavity()
    {
        if (swordType == SwordType.Bounce)
            gravityScale = bounceGravity;
        else if (swordType == SwordType.Pierce)
            gravityScale = priceGravity;
        else if (swordType == SwordType.Spin)
            gravityScale = spinGravity;
    }

    protected override void Update()
    {
        if (Input.GetMouseButtonUp(1))
        {

            finalDir = new Vector2(AimDir().normalized.x * launchForce.x, AimDir().normalized.y * launchForce.y);
        }

        if (Input.GetKey(KeyCode.Mouse1))
        {
            for (int i = 0; i < dots.Length; i++)
            {
                dots[i].transform.position = DotsPosition(i * disBetweenDots);
            }
            dir = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - player.transform.position.x;
            player.FlipController(dir);

        }
    }



    public void CreateSword()
    {
        GameObject go = Instantiate(swordPrefab, player.launchSwordPos.position, transform.rotation);
        SwordController sc = go.GetComponent<SwordController>();

        if (swordType == SwordType.Bounce)
        {
            sc.SetupBounce(true, bounceAmount, bounceSpeed, freezeTime);
            PlaySwordSound(E_SoundType.RotateSword);
        }
        else if (swordType == SwordType.Pierce)
        {
            sc.SetupPierce(pierceAmount);
            PlaySwordSound(E_SoundType.NormalSword);
        }
        else if (swordType == SwordType.Spin)
        {
            sc.SetupSpin(true, maxTravelDistance, spinDuration, hitCooldown);
            PlaySwordSound(E_SoundType.RotateSword);
        }
        else
        {
            PlaySwordSound(E_SoundType.NormalSword);
        }

        sc.SetSword(finalDir, gravityScale, player,10f);
        DotsActive(false);
        player.SetSword(go);
    }

    void PlaySwordSound(E_SoundType type)
    {
        AudioManager.Instance.PlaySound((int)(type));
    }

    public override void UseSkill()
    {
        base.UseSkill();
    }

    #region Aim    
    public Vector2 AimDir()
    {
        Vector2 playerPos = player.launchSwordPos.position;
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 dir = mousePos - playerPos;
        return dir;
    }

    public void DotsActive(bool active)
    {
        for (int i = 0; i < dots.Length; i++)
        {
            dots[i].SetActive(active);
        }
    }

    void CreateDots()
    {
        dots = new GameObject[dotsNum];
        for (int i = 0; i < dotsNum; i++)
        {
            dots[i] = Instantiate(dotPrefab, dotsParent);
            dots[i].SetActive(false);
        }
    }

    Vector2 DotsPosition(float t)
    {
        Vector2 pos = (Vector2)player.launchSwordPos.position + new Vector2(
            AimDir().normalized.x * launchForce.x,
            AimDir().normalized.y * launchForce.y) * t + .5f * (Physics2D.gravity * gravityScale) * (t * t);
        return pos;
    }
    #endregion
}
